package com.ys.controller.api.outer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ys.service.business.TpAmazonShopService;
import com.ys.service.business.TpAnalysisDailyTargetService;

@Controller
@RequestMapping("/api/outer/notify")
public class ApiOuterNotifyController {
	private static final Logger logger = LoggerFactory.getLogger(ApiOuterNotifyController.class);
	
	@Autowired
	private TpAnalysisDailyTargetService tpAnalysisDailyTargetService;
    
    @Autowired
    private TpAmazonShopService tpAmazonShopService;

	/*
	 * 更新运营日报表近14天数据
	 */
	@RequestMapping("/addTpAnalysisDailyTargetByShop")
	@ResponseBody
	public void addTpAnalysisDailyTargetByShop() {
		logger.info("***********触发更新销售日报表（运营日报表）近14天数据-开始***********");
		try {
			for(int i=1;i<=14;i++){
				//获取昨天时间日期
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DAY_OF_MONTH, -i);
				String agoDay = sdf.format(calendar.getTime());
				logger.info("***********刷新：第"+i+"天，"+agoDay+" 记录***********");
				tpAnalysisDailyTargetService.addByDay(agoDay);

			}
			logger.info("***********触发更新销售日报表（运营日报表）近14天数据-结束***********");
		}catch (Exception e){
			logger.error("更新运营日报表近14天数据出错：",e.getMessage(),e);
		}
	}

	/*
	 * 原请求路径：
	 * https://api.mini-ease.com/api/amazon/amazonAuthCallback
	 * 测试请求路径
	 * https://admintest.mini-ease.com/api/outer/notify/notifyFromAmazonSpApi
	 * 来自亚马逊SPAPI的授权通知
	 * 参数格式示例：
	 * ?spapi_oauth_code=RHUHNVdSmOPMjFqvahHm&state=6ffd99eb-2be6-4dba-9487-9c5242adba16&selling_partner_id=A1HQDY94T3ZSR8
	 */
	@RequestMapping("/notifyFromAmazonSpApi")
	public String notifyFromAmazonSpApi(Model model,
			@RequestParam Map<String, Object> params,
			String spapi_oauth_code, // 如：RHweFpBzliAbIVshSEyM
			Long state, // 如：6ffd99eb-2be6-4dba-9487-9c5242adba16
			String selling_partner_id // 如：A1HQDY94T3ZSR8
			) {
    	logger.info("通知内容param---" + JSON.toJSONString(params));
		logger.info("***********来自亚马逊SPAPI的授权通知-【start】***********");
		Long shopId = state;
    	tpAmazonShopService.getAmazonToken(spapi_oauth_code, shopId);

		logger.info("***********来自亚马逊SPAPI的授权通知-【end】***********");
		
        model.addAttribute("message", "亚马逊店铺授权成功!");
		return "business/tpAmazonShop/amazontokenauth";
	}

	/*
	 * 原请求路径：
	 * https://api.mini-ease.com/api/amazon/amazonAuthCallbackGg
	 * 测试请求路径
	 * https://admintest.mini-ease.com/api/outer/notify/notifyFromAmazonAdsApi
	 * 来自亚马逊SPAPI的授权通知
	 * 参数格式示例：
	 * ?spapi_oauth_code=RHUHNVdSmOPMjFqvahHm&state=6ffd99eb-2be6-4dba-9487-9c5242adba16&selling_partner_id=A1HQDY94T3ZSR8
	 */
	@RequestMapping("/notifyFromAmazonAdsApi")
	public String notifyFromAmazonAdsApi(Model model,
			@RequestParam Map<String, Object> params,
			String code, // 如：RHweFpBzliAbIVshSEyM
			Long state // 如：6ffd99eb-2be6-4dba-9487-9c5242adba16
			) {
    	logger.info("通知内容param---" + JSON.toJSONString(params));
		logger.info("***********来自亚马逊广告API的授权通知-【start】***********");
		Long shopId = state;
    	tpAmazonShopService.getAmazonTokenGg(code, shopId);

		logger.info("***********来自亚马逊广告API的授权通知-【end】***********");
		
        model.addAttribute("message", "亚马逊店铺授权成功!");
		return "business/tpAmazonShop/amazontokenauth";
	}

	/*
	 * 来自1688开放平台的通知
	 * 测试请求路径
	 * https://admintest.mini-ease.com/api/outer/notify/notifyFrom1688
	 * 参数格式示例：
	 */
	@RequestMapping("/notifyFrom1688")
    @ResponseBody
	public String notifyFrom1688(@RequestParam Map<String, Object> params) {
    	logger.info("通知内容param---" + JSON.toJSONString(params));
		logger.info("***********来自1688开放平台的通知-【start】***********");

		logger.info("***********来自1688开放平台的通知-【end】***********");
		
		return "200";
	}

    /**
     * 亚马逊token回调显示页面
     */
    @RequestMapping("/amazontokenauth")
    public String amazontokenauth(Model model, String auth){
        if (auth == null || auth.length() == 0){
            auth = "no";
        }
        String message = "";
        if (auth.contains("yes")){
            message = "亚马逊店铺授权成功!";
        }else{
            message = "亚马逊店铺授权失败!";
        }
        model.addAttribute("message", message);
		return "business/tpAmazonShop/amazontokenauth";
    }
}
